
$(function(obj){
//1 刷新左侧ztree
	flashTreeNode();

//2 绑定添加兄弟节点按钮
    $('.addBrotherPrivilege').click(function(){
    	addBroPrivilege();
    });
//3 绑定添加子类节点按钮
	$('.addChildPrivilege').click(function(){
    	addChiPrivilege();
    });
    
//4绑定全选框
	$('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    });
    
    //5.删除选定的，多项删除
    $('.del_select_btn').click(function(){
    	del_select_func(this);
    });



});

//分页查询
function paging(pageNo){
	var pageSize = $("#pageSize  option:selected").text();
	var treeNodeId=$("#treeId").val();
	$.ajax({
		url: "pagingPrivilegeManagement.do",
		type:"post",
		dataType: 'json',
		data:{
			"iPageSize":pageSize,
			"iPageNo":pageNo,
			"priId":treeNodeId
			},
		error:function(data){  
			layer.msg('出错了！！',{time:2000});
	    },
		success: function(jsons) {
			if(jsons!=null){
				$("#pageTable").html("");
				$("#pageFooter").html("");
				var pageList=jsons.pageData.pageList;
				var sysPrivilege=jsons.sysPrivilege;
				var priPidName='<td></td>';
				if(pageList != null && pageList != ""){
					for(var i=0;i<pageList.length;i++ ){
					for(var j=0;j<sysPrivilege.length;j++){
						if(pageList[i].priPid==sysPrivilege[j].priId){
							priPidName='<td title='+(sysPrivilege[j].priName==null?'':sysPrivilege[j].priName)+'>'+(sysPrivilege[j].priName==null?'':sysPrivilege[j].priName)+'</td>';
						}
					}
					var partsForm=
						'<tr id="priId" priId="'+pageList[i].priId +'">' +
		                    '<td class="checkNot"><input type="checkbox" value="' + pageList[i].priId + '" class="checkinput" name="checkradio" /></td>' +
		                    '<td title='+(pageList[i].priName==null?'':pageList[i].priName)+'>'+(pageList[i].priName==null?'':pageList[i].priName)+'</td>'+
		                    priPidName+
		                    '<td title='+(pageList[i].progCode==null?'':pageList[i].progCode)+'>'+(pageList[i].progCode==null?'':pageList[i].progCode)+'</td>'+
		                    '<td title='+(pageList[i].priPrior==null?'':pageList[i].priPrior)+'>'+(pageList[i].priPrior==null?'':pageList[i].priPrior)+'</td>'+
		                    '<td title='+(pageList[i].priType=='9fa'?'根节点':pageList[i].priType=='9fb'?'一级菜单':pageList[i].priType=='9fc'?'目录菜单':pageList[i].priType=='9fd'?'功能菜单':pageList[i].priType)+'>'+(pageList[i].priType=='9fa'?'根节点':pageList[i].priType=='9fb'?'一级菜单':pageList[i].priType=='9fc'?'目录菜单':pageList[i].priType=='9fd'?'功能菜单':pageList[i].priType)+'</td>'+
		                    '<td title='+(pageList[i].showHideFlag==0?'显示':'隐藏')+'>'+(pageList[i].showHideFlag==0?'显示':'隐藏')+'</td>'+
		                    '<td title='+(pageList[i].state=='9ga'?'开启':pageList[i].state=='9gb'?'关闭':pageList[i].state)+'>'+(pageList[i].state=='9ga'?'开启':pageList[i].state=='9gb'?'关闭':pageList[i].state)+'</td>'+
		                    '<td title='+(pageList[i].remark==null?'':pageList[i].remark)+'>'+(pageList[i].remark==null?'':pageList[i].remark)+'</td>'+
		                    '<td><i class="Edit content_current edit_btn" title="编辑"></i>' +
                        	'<i class="Del content_current delete_btn" title="变为不可用"></i>' +
		                '</tr>';
			    	$(partsForm).appendTo("#pageTable");
				}
				}
				
				
				// 绑定删除按钮
    			$('.delete_btn').click(function(){
        			delete_func(this);
    			});

				// 绑定编辑按钮
    			$('.edit_btn').click(function(){
        			edit_func(this);
    			});
				
				var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);
				
			}
				
		}
	});
}

//添加兄弟节点
	function addBroPrivilege(){
		var priId=$("#treeId").val();
		var privilegeName = document.getElementById("privilegeName").innerHTML;
		if(privilegeName == "" || privilegeName == null){
			layer.msg('先选择权限节点，再点击添加按钮！', {time:2000});
		}else if(priId == null || priId == ""){
			layer.msg('该节点已经删除，请重新选择节点！', {time:2000});
		}else{
			layer.open({
				extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
	            type:2,
	            title:'添加同级节点权限',
	            area:['600px','400px'],
	            content:'addEditPrivilege.do?opera=add_bro&priId='+priId,
	            maxmin: true,
	            shadeClose: true
        	});
		}
	}

//添加子类节点
function addChiPrivilege(){
			var priId=$("#treeId").val();
			var privilegeName = document.getElementById("privilegeName").innerHTML;
			if(privilegeName == "" || privilegeName == null){
				layer.msg('先选择权限节点，再点击添加按钮！', {time:2000});
			}else if(priId == null || priId == ""){
				layer.msg('该节点已经删除，请重新选择节点！', {time:2000});
			}else{
		        layer.open({
		        	extend:['skin/layer_iframe.css'],
		            skin:'layer-ext-skin-layer_iframe.css',
		            type:2,
		            title:'添加子级节点权限',
		            area:['600px','400px'],
		            content:'addEditPrivilege.do?opera=add_chi&priId='+priId,
		            maxmin: true,
		            shadeClose: true
		        });
		    }
	}

//添加或修改删除权限刷新左侧树
function flashTreeNode(){

	var setting = {
		data : {
			key : {
				url : "u"
			},
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : onClick
		}
	};

	$.ajax({
		type: 'POST',
		dataType : "json",
		data:{
            },
		url: "initLookPrivilege.do",//请求的action路径
		error: function () {//请求失败处理函数
			layer.msg('请求失败！',{time:2000});
		},
		success:function(result){ //请求成功后处理函数。  
			var treeNodes = eval("("+result.lstTree+")");
			//var treeNodes = result.lstTree;   //把后台封装好的简单Json格式赋给treeNodes
			$.fn.zTree.init($("#treeWay"), setting, treeNodes); 
		}
	});
	
	function onClick(event, treeId, treeNode, clickFlag){
		$("#treeId").val("");
		$("#treeId").val(treeNode.id);
		document.getElementById("privilegeName").innerHTML=treeNode.name;
		$('.checkAll').removeAttr('checked');
		paging(1);
	}
}

function edit_func(obj){
    var priId=$(obj).parent().parent().attr('priId');
    layer.open({
    	extend:['skin/layer_iframe.css'],
		skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改权限页面',
        area:['600px','400px'],
        content:'addEditPrivilege.do?opera=edit&priId='+priId,
        maxmin: true,
        shadeClose: true
    });
}

function delete_func(obj){
    var priId=$(obj).parent().parent().attr('priId');
    if(confirm('确定把该权限变为不可用状态?')){
        $.ajax({
            url:"deletePrivilege.do",
            type:"post",
            dataType: 'json',
            data:{
                "priId":priId
            },
            beforeSend:function(){
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('已经改为关闭状态', {time:1300});
                    flashTreeNode();
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('改为不可用失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

function del_select_func(obj){
	var checkedDel = "";
	 var arr=document.getElementsByName("checkradio");
	 	
	 	for(i=0;i<arr.length;i++){
			if(arr[i].checked){
			var a = arr[i].value;
			checkedDel+=a+",";
			}
		}
		if(checkedDel==""){
			layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
		}else{
			if(confirm('确定把所有选中的权限设置为不可用状态?')){
			$.ajax({
            url:"deleteSelectPrivilege.do",
            type:"post",
            dataType: 'json',
            data:{
                "checkedDel":checkedDel
            },
            beforeSend:function(){
                
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                	flashTreeNode();
                    layer.msg('已经改为关闭状态', {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('改为不可用失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
			}
		}
}




